<template>
  <el-dialog v-model="dialogFormVisible" class="dialog-title" :title="title" destroy-on-close>
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="margin-right:50px;margin-left:50px;">
      <el-form-item label="上级部门" prop="parentIds">
        <el-cascader
          v-model="temp.parentId"
          :options="list"
          :props="treeProps"
          :show-all-levels="false"
          label="name"
          change-on-select
          clearable
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="temp.name" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="temp.remarks" :autosize="{ minRows: 2, maxRows: 4}" type="textarea"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button v-permission="['sys:organization:update']" type="primary" :loading="loading" @click="title==='新增'?createData():updateData()">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { createOrganization, fetchOrganization, updateOrganization } from '@/api/sys/organization/organization'

export default {
  name: 'OrganizationForm',
  emits: ['refreshList'],
  data() {
    return {
      rules: {
        name: [{ required: true, message: '名称为必填项' }]
      },
      list: [],
      temp: {},
      title: undefined,
      parentIds: undefined,
      treeProps: {
        value: 'id',
        label: 'name',
        checkStrictly: true,
        emitPath: false,
        expandTrigger: 'hover'
      },
      loading: false,
      dialogFormVisible: false
    }
  },
  methods: {
    /**
     * 刷新列表
     * @param data 刷新列表数据
     */
    refreshList(data) {
      this.$emit('refreshList', data)
    },
    /**
     * 设置列表
     */
    setList(list) {
      this.list = list
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.loading = false
      this.parentIds = undefined
      this.temp = {
        id: undefined,
        name: undefined,
        parentId: undefined,
        remark: undefined
      }
    },
    /**
     * 新增
     */
    handleCreate() {
      this.resetTemp()
      this.title = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 提交新增
     */
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          createOrganization(this.temp).then((response) => {
            this.loading = false
            if (response.data.success) {
              this.dialogFormVisible = false
              const data = response.data.data
              this.refreshList(data)
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    /**
     * 编辑
     * @param id 待编辑的id
     */
    handleUpdate(id) {
      this.resetTemp()
      this.title = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
      fetchOrganization(id).then(response => {
        if (response.data.success) {
          this.temp = response.data.data // copy obj
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    /**
     * 提交编辑
     */
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          updateOrganization(this.temp).then((response) => {
            this.loading = false
            if (response.data.success) {
              this.dialogFormVisible = false
              // console.log(this.temp, 'this.temp')

              this.refreshList(this.temp)
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
